//
//  oldForgetPasswordViewController.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/2/28.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldForgetPasswordViewController.h"
#import "oldRegex.h"

@interface oldForgetPasswordViewController ()
{
    NSUInteger next_time;
}
@property (weak, nonatomic) IBOutlet UITextField *text_phoneNumber;
@property (weak, nonatomic) IBOutlet UIButton *btn_validcode;
@property (weak, nonatomic) IBOutlet UITextField *text_password;
@property (weak, nonatomic) IBOutlet UITextField *text_validcode;
@property (weak, nonatomic) IBOutlet UITextField *text_confirm;
@property (weak, nonatomic) IBOutlet UIButton *btn_commit;
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation oldForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    _btn_commit.layer.cornerRadius = 6.f;
    _btn_validcode.layer.cornerRadius = 6.f;
    [_btn_commit addTarget:self action:@selector(btn_commit_tapped:) forControlEvents:UIControlEventTouchDown];
    [_btn_validcode addTarget:self action:@selector(sendSMS:) forControlEvents:UIControlEventTouchDown];
}

- (void)btn_commit_tapped:(id)sender
{
    if (![oldRegex isMobileNo:_text_phoneNumber.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    if (![oldRegex isSMSCode:_text_validcode.text]) {
        [oldHub warningUser:@"请输入正确的验证码"];
        return;
    }
    
    if (![oldRegex isValidatePassword:_text_password.text]) {
        [oldHub warningUser:@"请输入数字或者字母组合的密码"];
        return;
    }
    
    if (_text_password.text != _text_confirm.text) {
        [oldHub warningUser:@"两次密码输入不同"];
        return;
    }
    
    NSString *password = [oldRegex md5:_text_password.text];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: password, @"password", _text_validcode.text, @"code", nil];
    
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_ForgetPWD params:params success:^(NSDictionary *dic) {
        [oldHub hideHUD];
        [oldHub warningUserAfterJump:@"密码修改成功" time:1];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } error:^(NSString *error) {
        [oldHub hideHUD];
        [oldHub warningUser:error];
    }];
}

- (void)sendSMS:(id)sender
{
    if (![oldRegex isMobileNo:_text_phoneNumber.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    [self delayForGetCode];
    
    [oldHub waiting:@"验证码发送中"];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:_text_phoneNumber.text forKey:@"mobile"];
    
    [OLDNetWork httpRequestWithPostPut:URL_Register_SMS params:dict success:^(NSDictionary *dic) {
        [oldHub hideHUD];
        
    } error:^(NSString *error) {
        [oldHub hideHUD];
        [oldHub warningUser:error];
    }];
}

#pragma mark - timer
- (void)delayForGetCode
{
    next_time = 60;
    
    [self SmsCanClick:NO];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setDelay:) userInfo:_timer repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
    [_timer fire];
}

- (void)setDelay:(id)sender
{
    next_time--;
    if (next_time == 0) {
        [self SmsCanClick:YES];
        
        _timer.fireDate = [NSDate distantFuture];
        [_timer invalidate];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"重新发送(%ld)", (long)next_time];
    _btn_validcode.titleLabel.text = str;
    [_btn_validcode setTitle:str forState:UIControlStateNormal];
}

- (void)SmsCanClick:(BOOL)can
{
    if (can) {
        _btn_validcode.userInteractionEnabled = YES;
        _btn_validcode.alpha = 1.0;
        _btn_validcode.titleLabel.text = @"获取验证码";
        [_btn_validcode setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else
    {
        _btn_validcode.userInteractionEnabled = NO;
        _btn_validcode.alpha = 0.4;
    }
}

@end
